package com.finch.juc;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Levi Wang
 * @create 2021-07-07 22:31
 *
 *
 * 多线程之间按顺序调用，实现A->B->C
 * 三个线程启动，要求如下
 *
 * AA打印5次，BB打印10次，CC打印15次
 * 接着
 * AA打印5次，BB打印10次，CC打印15次
 * 。。。。。。来10轮
 *
 * 这里与之前的区别就是，只唤醒一个，精准打击，因为要保证ABC的顺序
 *
 *  1  高内聚低耦合前提下，线程操作资源类
 *
 *  2  判断、干活、通知（线程通信、生产者消费者模型）
 *
 *  3  多线程交互中，必须要防止多线程的虚假唤醒，也即（解决办法，在判断的时候不要用if ，要用while防止虚假唤醒）
 *  交互就是指有 wait 和 notify
 *
 *  4  标志位
 *
 *
 *
 * 方法二：
 * join（）方法  参考 https://www.cnblogs.com/lcplcpjava/p/6896904.html
 *
 *
 */

class ShareResource{
    private int number = 1;//标志位 1:A 2:B 3:C
    private Lock lock = new ReentrantLock();
    private Condition condition1 = lock.newCondition();
    private Condition condition2 = lock.newCondition();
    private Condition condition3 = lock.newCondition();

    public void print5() throws Exception {
        lock.lock();
        try {
            //判断
            while(number != 1){
                condition1.await();
            }
            //干活
            for(int i = 1; i <= 5; i++){
                System.out.println(Thread.currentThread().getName() + "\t" + i);
            }
            //通知
            number = 2;//修改标志位
            condition2.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }


    public void print10() throws Exception {
        lock.lock();
        try {
            //判断
            while(number != 2){
                condition2.await();
            }
            //干活
            for(int i = 1; i <= 10; i++){
                System.out.println(Thread.currentThread().getName()+ "\t" + i);
            }
            //通知
            number = 3;//修改标志位
            condition3.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }


    public void print15() throws Exception {
        lock.lock();
        try {
            //判断
            while(number != 3){
                condition3.await();
            }
            //干活
            for(int i = 1; i <= 15; i++){
                System.out.println(Thread.currentThread().getName()+ "\t" + i);
            }
            //通知
            number = 1;//修改标志位
            condition1.signal();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }


}




public class ThreadOrderAccess {

    public static void main(String[] args) {
        ShareResource shareResource = new ShareResource();

         new Thread(() -> {
             for(int i = 1; i <= 10; i++){ //这里是因为题目中要求来10轮
                 try {
                     shareResource.print5();
                 } catch (Exception e) {
                     e.printStackTrace();
                 }
             }
         }, "A").start();

        new Thread(() -> {
            for(int i = 1; i <= 10; i++){ //这里是因为题目中要求来10轮
                try {
                    shareResource.print10();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, "B").start();

        new Thread(() -> {
            for(int i = 1; i <= 10; i++){ //这里是因为题目中要求来10轮
                try {
                    shareResource.print15();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, "C").start();



    }

}


















